package com.chengyu.core.controller.walk;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.alibaba.fastjson.JSONArray;
import com.chengyu.core.component.OperationLog;
import com.chengyu.core.controller.ShopBaseController;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.form.TrendsForm;
import com.chengyu.core.domain.form.WalkTrendsBatchAdd;
import com.chengyu.core.domain.result.WalkTrendsCommentResult;
import com.chengyu.core.domain.result.WalkTrendsResult;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.WalkTrends;
import com.chengyu.core.model.WalkTrendsDownload;
import com.chengyu.core.model.WalkTrendsDownloadNum;
import com.chengyu.core.service.walk.WalkTrendsDownloadNumService;
import com.chengyu.core.service.walk.WalkTrendsDownloadService;
import com.chengyu.core.service.walk.WalkTrendsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "逛逛社区")
@Controller
@RequestMapping("/member")
public class WalkTrendsController extends ShopBaseController {

    @Autowired
    private WalkTrendsService walkTrendsService;
    @Autowired
    private WalkTrendsDownloadNumService walkTrendsDownloadNumService;
    @Autowired
    private WalkTrendsDownloadService walkTrendsDownloadService;

    @ApiOperation(value = "动态列表")
    @ResponseBody
    @RequestMapping(value = "/trends/getList", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsResult>> getList(TrendsForm form,
                                                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                              @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
        form.setShopId(getCurrentShopId());
        CommonPage<WalkTrendsResult> list = walkTrendsService.getTrendsList(null,1,true,form.getGoodsId(), null, form, page, pageSize);
        return CommonResult.success(list);
    }

    @OperationLog
    @ApiOperation(value = "发布动态")
    @ResponseBody
    @RequestMapping(value = "/trends/publish", method = RequestMethod.POST)
    public CommonResult<String> add(WalkTrends trends, String goodsIds) throws ServiceException {
        trends.setLocation(1);
        List<Integer> goodsIdList = null;
        if (com.chengyu.core.utils.StringUtils.isNotBlank(goodsIds)) {
            goodsIdList = Convert.convert(new TypeReference<List<Integer>>() {
            }, goodsIds.split(CommonConstant.DH_REGEX));
        }
        if (trends.getId() == null) {
            trends.setShopId(getCurrentShopId());
            walkTrendsService.publishTrends(getCurrentWalkMember(), trends, goodsIdList);
        } else {
            walkTrendsService.editTrends(trends, goodsIdList);
        }
        return CommonResult.success(null);
    }

    @OperationLog
    @ApiOperation(value = "发布动态-批量")
    @ResponseBody
    @RequestMapping(value = "/trends/publishBatch", method = RequestMethod.POST)
    public CommonResult<String> publishBatch(String trendsList) throws ServiceException {
        List<WalkTrendsBatchAdd> list = new ArrayList<>(JSONArray.parseArray(trendsList, WalkTrendsBatchAdd.class));
        Integer shopId = getCurrentShopId();
        walkTrendsService.publishTrendsBatch(getCurrentWalkMember(), list,shopId);
        return CommonResult.success(null);
    }


    @OperationLog
    @ApiOperation(value = "删除动态")
    @ResponseBody
    @RequestMapping(value = "/trends/delete", method = RequestMethod.POST)
    public CommonResult<String> delete(String ids) throws ServiceException {
        if (StringUtils.isEmpty(ids)) {
            return CommonResult.failed("请至少选择一条数据");
        }
        walkTrendsService.deleteTrends(ids);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "动态评论列表")
    @ResponseBody
    @RequestMapping(value = "/trends/getCommentList", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsCommentResult>> getCommentList(Integer trendsId,
                                                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
        CommonPage<WalkTrendsCommentResult> list = walkTrendsService.getTrendsCommentList(trendsId, null, null, null, page, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "获取下载次数申请列表")
    @ResponseBody
    @RequestMapping(value = "/trends/getTrendsDownloadNumApply", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsDownloadNum>> getTrendsDownloadNumApply(Integer source,Date dateFrom, Date dateTo,Integer status, Integer type, Integer walkMemberId,
                                                                                     @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                                     @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
        Integer shopId = this.getCurrentShopId();
        CommonPage<WalkTrendsDownloadNum> list = walkTrendsDownloadNumService.listDownloadNum(source, dateFrom,  dateTo,shopId, status, type, walkMemberId, page, pageSize,null);
        return CommonResult.success(list);
    }
    @ApiOperation(value = "获取下载次数申请审核")
    @ResponseBody
    @RequestMapping(value = {"/trends/editVerifyTrendsDownloadNum"}, method = RequestMethod.POST)
    public CommonResult<WalkTrendsDownloadNum> editVerifyTrendsDownloadNum(WalkTrendsDownloadNum walkTrendsDownloadNum) throws ServiceException {
        walkTrendsDownloadNumService.editDownloadNum(walkTrendsDownloadNum);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "动态下载列表")
    @ResponseBody
    @RequestMapping(value = "/trends/getDownloadList", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsDownload>> getDownloadList(Integer type, Date dateFrom, Date dateTo,Integer downloadWalkMemberId,
                                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                        @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
        CommonPage<WalkTrendsDownload> list = walkTrendsDownloadService.listDownloadLog(dateFrom,dateTo,type, getCurrentShopId(),downloadWalkMemberId, page, pageSize);
        return CommonResult.success(list);
    }

    @OperationLog
    @ApiOperation(value = "重新生成水印")
    @ResponseBody
    @RequestMapping(value = "/trends/createWatermark", method = RequestMethod.POST)
    public CommonResult<String> createWatermark(String trendsLists) throws Exception {
        List<WalkTrends> trendsList = new ArrayList<>(JSONArray.parseArray(trendsLists, WalkTrends.class));
        if (StringUtils.isEmpty(trendsList)) {
            return CommonResult.failed("请至少选择一条数据");
        }
        walkTrendsService.createWatermark(trendsList);
        return CommonResult.success(null);
    }


    @ApiOperation(value = "修改素材动态")
    @ResponseBody
    @RequestMapping(value = {"/trends/editTrends"}, method = RequestMethod.POST)
    public CommonResult<WalkTrendsDownloadNum> editTrends(WalkTrends walkTrends) throws ServiceException {
        walkTrendsService.editTrendsContent(walkTrends);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "根据商品删除素材")
    @ResponseBody
    @RequestMapping(value = {"/trends/deleteTrendsByGoodsId"}, method = RequestMethod.GET)
    public CommonResult<WalkTrendsDownloadNum> deleteTrendsByGoodsId(Integer goodsId) throws ServiceException {
        walkTrendsService.deleteTrendsByGoodsId(goodsId);
        return CommonResult.success(null);
    }
    @ApiOperation(value = "根据商品删除素材")
    @ResponseBody
    @RequestMapping(value = {"/trends/deleteTrendsByDown"}, method = RequestMethod.GET)
    public CommonResult<WalkTrendsDownloadNum> deleteTrendsByDown() throws ServiceException {
        walkTrendsService.deleteTrendsByDown();
        return CommonResult.success(null);
    }
    @ApiOperation(value = "根据素材id，会员id查看下载记录")
    @ResponseBody
    @RequestMapping(value = {"/trends/getDownLoadInfo"}, method = RequestMethod.GET)
    public CommonResult<WalkTrendsDownload> getDownLoadInfo(Integer memberId,Integer trendsId) throws ServiceException {
        CommonResult<WalkTrendsDownload> downLoadInfo = walkTrendsDownloadService.getDownLoadInfo(memberId, trendsId);
        return downLoadInfo;
    }
}
